﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SiGeSIB.GUI.Properties;

namespace SiGeSIB.GUI.Common.DedaGrid
{
    public partial class UGroupingGrid : UserControl
	{
		#region Auxiliar Structs
		public enum EGroupState
		{
			Expanded,
			Retracted
		}
		#endregion
		#region Constructor
		public UGroupingGrid()
		{
			InitializeComponent();

			uDedaGrid.Owner = this;
			uDedaGrid.SelectionChanged += new EventHandler(uDedaGrid_SelectionChanged);
		}
		#endregion

		#region Attributes
		private EGroupState _State;
		#endregion

		#region Properties
		[DefaultValue("Title")]
		public string Caption
		{
			get { return grpGrid.Text; }
			set { grpGrid.Text = value; }
		}

		[Browsable(false)]
		public UDedaGrid Owner 
		{
			get;
			set;
		}
		#endregion

        #region Methods
		#region Utils
		public void SetDataBinding(DataTable dt, List<string> groupedColumns)
		{
			uDedaGrid.SetDataBinding(dt, groupedColumns);
			SetHeight();
		}
		public List<DataRow> GetSelectedRows()
		{
			return
				uDedaGrid.GetSelectedRows();
		}
		public void ApplyFilter(string s)
		{
			uDedaGrid.ApplyFilter(s);
		}
		#endregion

		#region Focus
		public void OnRequestFocus(UDedaGrid requester)
		{
			Owner.RequestFocus(requester);
		}
		public void Unfocus(UDedaGrid requester)
		{
			uDedaGrid.Unfocus(requester);
		}
		#endregion

		#region UIState
		private void _RefreshUIState()
		{
			uDedaGrid.Visible = _State == EGroupState.Expanded;
			lblRetract.Image =
				_State == EGroupState.Expanded ?
				Resources.expand_minus :
				Resources.expand_plus;

		}
		public void SetHeight()
		{
			_SetHeight(Size.Empty);
		}
		private void _SetHeight(Size newSize)
		{
			if (newSize.IsEmpty)
				newSize = new Size(uDedaGrid.Width + 20, uDedaGrid.Height + 28);

			this.Size = new Size(newSize.Width, newSize.Height);
			// é necessário redimensionar para garantir depois as âncoras
			uDedaGrid.AutoScrollMinSize =
				uDedaGrid.Size = new Size(this.Width -20 , this.Height - 28);
		}
		private void _Retract()
		{
			Size oldSize = new Size(uDedaGrid.Width, uDedaGrid.Height);

			_SetHeight(new Size(this.Width, 30));

			// Olhar pq o setar o owner, aumenta a largura do Auto-Size.
			Owner.SetHeight();

			uDedaGrid.Size = oldSize;
		}

		private void _Expand()
		{
			SetHeight();

			Owner.SetHeight();
		}
		#endregion
		#endregion

		#region Event Handler

		public event EventHandler SelectionChanged;
		private void _OnSelectionChanged()
		{
			if (SelectionChanged != null)
				SelectionChanged(this, new EventArgs());
		}

		void uDedaGrid_SelectionChanged(object sender, EventArgs e)
		{
			_OnSelectionChanged();
		} 
		private void lblRetract_Click(object sender, EventArgs e)
		{
			switch (_State)
			{
				case(EGroupState.Expanded):
					_Retract(); 
					_State = EGroupState.Retracted;
					break;
				case (EGroupState.Retracted):
					_Expand();
					_State = EGroupState.Expanded;
					break;
			}

			_RefreshUIState();
		}
		#endregion

        internal void ClearSelection()
        {
            uDedaGrid.ClearSelection();
        }
    }
}
